Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
end-of-stream
Advanced tools
Call a callback when a readable/writable/duplex stream has completed or failed.
The end-of-stream npm package is designed to help developers handle the end or error conditions of a stream more easily. It provides a simple way to execute a callback when a stream has finished or failed, making it easier to manage streams in Node.js applications.
Callback on stream end
This feature allows you to execute a callback function when the stream ends or encounters an error. The code sample demonstrates how to use end-of-stream with a file read stream.
const eos = require('end-of-stream');
const fs = require('fs');
const readStream = fs.createReadStream('file.txt');
eos(readStream, function(err) {
if (err) return console.log('Stream had an error or closed early');
console.log('Stream has ended');
});
Promise support
This feature demonstrates how to use end-of-stream with promises, providing a more modern approach to handling stream completion. It uses the `promisify` utility from Node.js to convert the eos callback pattern into a promise.
const eos = require('end-of-stream');
const fs = require('fs');
const { promisify } = require('util');
const eosPromise = promisify(eos);
const readStream = fs.createReadStream('file.txt');
eosPromise(readStream).then(() => console.log('Stream has ended')).catch(err => console.log('Stream had an error or closed early'));
Pump is a package that not only listens for the end of streams but also helps to pipe streams together and handles cleanup in case of a stream ending prematurely. It's more comprehensive than end-of-stream as it also manages the flow between streams.
A node module that calls a callback when a readable/writable/duplex stream has completed or failed.
npm install end-of-stream
Simply pass a stream and a callback to the eos
.
Both legacy streams, streams2 and stream3 are supported.
var eos = require('end-of-stream');
eos(readableStream, function(err) {
// this will be set to the stream instance
if (err) return console.log('stream had an error or closed early');
console.log('stream has ended', this === readableStream);
});
eos(writableStream, function(err) {
if (err) return console.log('stream had an error or closed early');
console.log('stream has finished', this === writableStream);
});
eos(duplexStream, function(err) {
if (err) return console.log('stream had an error or closed early');
console.log('stream has ended and finished', this === duplexStream);
});
eos(duplexStream, {readable:false}, function(err) {
if (err) return console.log('stream had an error or closed early');
console.log('stream has finished but might still be readable');
});
eos(duplexStream, {writable:false}, function(err) {
if (err) return console.log('stream had an error or closed early');
console.log('stream has ended but might still be writable');
});
eos(readableStream, {error:false}, function(err) {
// do not treat emit('error', err) as a end-of-stream
});
MIT
end-of-stream
is part of the mississippi stream utility collection which includes more useful stream modules similar to this one.
FAQs
Call a callback when a readable/writable/duplex stream has completed or failed.
The npm package end-of-stream receives a total of 28,063,926 weekly downloads. As such, end-of-stream popularity was classified as popular.
We found that end-of-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.